<template>
	<view>
		<cu-custom bgColor="bg-white" :isBack="true">
			<block slot="backText"></block>
			<block slot="content" v-if="type == 'money'">余额提现</block>
			<block slot="content" v-if="type == 'wallet'">钱包提现</block>
		</cu-custom>
		<block v-if="preview && preview.config.withdraw_is_open == '0'">
			<view class="text-gray text-center margin-xl">服务暂未开启</view>
		</block>
		<block v-else>
			<view style="background-color: rgb(254,39,114); " class=" overflow padding text-white text-center relative">
				<view class="margin-tb ">可提现金额</view>
				<view class="text-xl text-bold text-price" v-if="type == 'money'">{{preview?preview.account.user_money:0}}</view>
				<view class="text-xl text-bold text-price" v-if="type == 'wallet'">{{preview?preview.account.wait_integral:0}}</view>
				<view class="absolute blackbg1 loglg padding-xs padding-left-sm" @tap="goPage('/pages/member/withdraw/index')">
					<text class="cuIcon cuIcon-form"></text>提现记录
				</view>
			</view>
			<view class="bg-white padding">
				<view class="text-lg">提现金额</view>
				<view class="text-price margin-top text-lg space-between align-center ">
					<input placeholder="请输入提现金额" name="input" class="w100 padding-lr" type="number" step="0.01" v-model="formData.cash"></input>
					<view style="background-color: rgb(254,39,114); color: #FFFFFF;" class="cu-btn round sm  txbtn" @tap="setAll">全部提现</view>
				</view>
			</view>

			<view class="cu-form-group margin-top">
				<view class="title">提现方式</view>
				<picker @change="PaymentPickerChange" :value="withdraw_mode" :range="paymentPick" >
					<view class="picker" >
						{{payment_index>=0?paymentPick[payment_index]:'请选择支付方式'}}
					</view>
				</picker>
			</view>
			<view class="bg-white margin-top">
				
				<view class="cu-form-group " v-if="withdraw_mode==2">
					<view class="title">支付宝名字<text class="text-red text-lg">*</text></view>
					<input placeholder="请输入支付宝姓名" name="input" v-model="formData.apply_name" class="text-right"></input>
				</view>
				<view class="cu-form-group " v-if="withdraw_mode==2">
					<view class="title">支付宝手机号<text class="text-red text-lg">*</text></view>
					<input placeholder="请填写支付宝手机号" name="input" v-model="formData.apply_number" class="text-right"></input>
				</view>
				<view class="cu-form-group " v-if="withdraw_mode==0">
					<view class="title">微信号<text class="text-red text-lg">*</text></view>
					<input placeholder="请填写微信号" name="input" v-model="formData.wx_number" class="text-right"></input>
				</view>
				<view class="cu-form-group " v-if="withdraw_mode==1">
					<view class="title">姓名<text class="text-red text-lg">*</text></view>
					<input placeholder="请输入姓名" name="input" v-model="formData.realname" class="text-right"></input>
				</view>
				<view class="cu-form-group " v-if="withdraw_mode==1">
					<view class="title">银行卡号<text class="text-red text-lg">*</text></view>
					<input placeholder="请输入银行卡号" name="input" v-model="formData.account_number" class="text-right"></input>
				</view>
				<view class="cu-form-group " v-if="withdraw_mode==1">
					<view class="title">开户行<text class="text-red text-lg">*</text></view>
					<input placeholder="请输入开户行" name="input" v-model="formData.bank_name" class="text-right"></input>
				</view>
				<view class="cu-form-group " v-if="withdraw_mode==1">
					<view class="title">开户支行<text class="text-red text-lg">*</text></view>
					<input placeholder="请填写开户支行" name="input" v-model="formData.bank_name_other" class="text-right"></input>
				</view>
			</view>
			<view class="margin-top-xl margin-lr">
				<view  style="background-color: rgb(254,39,114); color: #FFFFFF;"  class="cu-btn lg w100  round shadow" @tap="onSubmit">确认提现</view>

			</view>
			<view class="text-center text-gray margin-top" v-if="type == 'money'">最低提现金额￥{{preview?preview.config.withdraw_lowest_money:'-'}},提现手续费：{{preview?preview.config.withdraw_commission:'-'}}%</view>
			<view class="text-center text-gray margin-top" v-if="type == 'wallet'">最低提现金额￥{{preview?preview.config.withdraw_lowest_money:'-'}},提现手续费：{{preview?preview.config.integral_commission:'-'}}%</view>
		</block>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				member: null,
				preview: null,
				CLOUDPATH: this.$config.cloudPath,
				theme: this.$config.THEME(),
				formData: {
					transfer_type: 1,
					cash: 0.00,
					realname: '',
					mobile: '',
					account_number: '',
					bank_name: '',
					bank_name_other: '',
					wx_number:'',
					apply_number:'',
					apply_name:'',
					type:'money'
				},
				type:'money',
				app_withdraw:[],
				app_withdrawkey:[],
				payment_index: 0,
				withdraw_mode:0,
				paymentPicker: ['微信', '银行卡','支付宝'],
				paymentPick:[],
			}
		},
		onLoad(options) {
			if (options.type) {
				this.type = options.type
				this.formData.type = options.type
			}
			this.formData.apply_name=this.$common.getCache('formData_apply_name')
			this.formData.apply_number=this.$common.getCache('formData_apply_number')
			this.formData.wx_number=this.$common.getCache('formData_wx_number')
			this.formData.realname=this.$common.getCache('formData_realname')
			this.formData.account_number=this.$common.getCache('formData_account_number')
			this.formData.bank_name=this.$common.getCache('formData_bank_name')
			this.formData.bank_name_other=this.$common.getCache('formData_bank_name_other')
			
			if (this.$common.loginAuth(true)) {}
		},
		onShow() {
			if (this.$common.loginAuth(true)) {
				this.$common.getMember().then((member) => {
					this.member = member
					this.getDetail()
				})
			}
		},
		methods: {
			onSubmit() {
				let formData = this.formData
				if (formData.cash <= 0) {
					this.$common.showError('请输入提现金额')
					return
				}
				if(this.type == 'money'){
					if (parseFloat(formData.cash) > parseFloat(this.preview.account.user_money) ) {
						this.$common.showError('余额不足')
						return
					}
					if (parseFloat(formData.cash) < parseFloat(this.preview.config.withdraw_lowest_money) ) {
						this.$common.showError('金额不得低于'+this.preview.config.withdraw_lowest_money)
						return
					}
				}else{
					if (parseFloat(formData.cash) > parseFloat(this.preview.account.wait_integral) ) {
						this.$common.showError('钱包不足')
						return
					}
					if (parseFloat(formData.cash) < parseFloat(this.preview.config.withdraw_lowest_money) ) {
						this.$common.showError('金额不得低于'+this.this.preview.config.withdraw_lowest_money)
						return
					}
				}
				if (this.withdraw_mode == 1) {
					formData.transfer_type = 1
					if (formData.realname=='') {
						this.$common.showError('请填写姓名')
						return
					}
					
					if (formData.account_number=='') {
						this.$common.showError('请输入银行卡号')
						return
					}
					if (formData.bank_name_other=='') {
						this.$common.showError('请输入银行名称')
						return
					}
					if (formData.bank_name=='') {
						this.$common.showError('请输入开户行')
						return
					}
					this.$common.setCache('formData_realname', formData.realname, 30 * 60)
					this.$common.setCache('formData_account_number', formData.account_number, 30 * 60)
					this.$common.setCache('formData_bank_name', formData.bank_name, 30 * 60)
					this.$common.setCache('formData_bank_name_other', formData.bank_name_other, 30 * 60)
					
				}
				if (this.withdraw_mode == 2) {
					formData.transfer_type = 2
					if (formData.apply_number=='') {
						this.$common.showError('请填写支付宝账号')
						return
					}
					if (formData.apply_name=='') {
						this.$common.showError('请填写支付宝名称')
						return
					}
					this.$common.setCache('formData_apply_name', formData.apply_name, 30 * 60)
					this.$common.setCache('formData_apply_number', formData.apply_number, 30 * 60)
					
				}
				if (this.withdraw_mode == 0) {
					formData.transfer_type = 0
					if (formData.wx_number=='') {
						this.$common.showError('请填写微信号')
						return
					}
					this.$common.setCache('formData_wx_number', formData.wx_number, 30 * 60)
					
				}
				
				uni.showLoading({
					title: '提交中...',
					mask: true
				})
				formData.withdraw_mode = this.withdraw_mode
				
				// return
				this.$http
					.post('withdraw', formData)
					.then((response) => {
						uni.hideLoading()
						this.$common.showSuccess('提交成功，请耐心等待审核', (r) => {
							this.getDetail()
						})
					})
					.catch((response) => {
						uni.hideLoading()
					})
			},
			getDetail() {
				uni.showLoading({
					title: '加载中...'
				})
				this.$http
					.get('withdraw_preview')
					.then((response) => {
						this.preview = response.data.data
						this.app_withdraw=this.preview.app_withdraw
						this.app_withdrawkey=this.preview.app_withdrawkey
						if(this.$common.getCache('withdraw_mode')){
							this.withdraw_mode=this.$common.getCache('withdraw_mode')
							this.payment_index=this.$common.getCache('payment_index')
						}else{
							if(this.preview.app_withdrawkey.length>0){
								console.log(this.preview.app_withdrawkey)
								this.withdraw_mode=this.preview.app_withdrawkey[0]
							}
						}
						
						this.paymentPick=[];
						for(let i=0;i<this.paymentPicker.length;i++){
							
							if(this.app_withdraw.includes(i)){
								this.paymentPick.push(this.paymentPicker[i])
							}
						}
					
						uni.hideLoading()
					})
					.catch((response) => {
						uni.hideLoading()
					})

			},
			setAll() {
				if(this.type == 'wallet'){
					this.formData.cash = this.preview.account.wait_integral
				}else{
					this.formData.cash = this.preview.account.user_money
				}
				
			},
			PaymentPickerChange(e) {
				this.payment_index = e.detail.value
				this.$common.setCache('payment_index', this.payment_index, 30 * 60)
				this.withdraw_mode=this.app_withdrawkey[this.payment_index]
				this.$common.setCache('withdraw_mode', this.withdraw_mode, 30 * 60)
			}
		},
	}
</script>

<style>
	.txbtn {
		min-width: 160rpx;
	}

	.loglg {
		right: 0;
		top: 30rpx;
		border-radius: 30rpx 0 0 30rpx;
	}
</style>
